package com.gmrz.uap.facet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.ServiceSupport;
import com.gmrz.uap.model.Dict;
import com.gmrz.uap.model.Facets;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.List;

public class FacetService extends ServiceSupport {

    public static final FacetService me = new FacetService();


    /**
     * 获取所有应用ID信息
     *
     * @return
     */
    public List<Facets> findAll() {
        return Facets.dao.findAll();
    }


    public Facets findById(String id) {
        return Facets.dao.findById(id);
    }


    /**
     * 返回facets.uaf文件流
     */
    public BufferedReader getFacetsFileStream(String tenantId) {
        return new BufferedReader(new StringReader(getFacetsFileString2(tenantId)));
    }


    /**
     * 根据id删除应用
     *
     * @param ids 应用id
     */
    @Before(Tx.class)
    public boolean remove(String ids, String tanentId) {
        String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);

        boolean flag = false;

        for (String id : idArr) {
            flag = Facets.dao.deleteById(id);
        }

        return flag;
    }

    /**
     * 根据页面传过来的条件进行分页查询
     *
     * @param facets 应用信息
     * @param page   页号
     * @param rows   行数
     * @param sort   排序字段
     * @param order  升序/降序
     * @return 分页结果 Page<Facets>
     */
    public Page<Facets> findListPaginate(Facets facets, int page, int rows, String sort, String order, String tenantid) {
        return Facets.dao.findListPaginate(facets, page, rows, sort, order, tenantid);
    }

    /**
     * @return facets.uaf文件json串
     */
    private String getFacetsFileString(String tanentid) {
        List<Facets> facetses = Facets.dao.findAll(tanentid);
        StringBuilder sb = new StringBuilder();
        //sb.append("{\"trustedFacets\":[{ \"version\": {\"major\": 1,\"minor\": 0},\"ids\": [");
        sb.append("{\n" +
                "  \"trustedFacets\":[\n" +
                "      { \"version\": {\n" +
                "            \"major\": 1,\n" +
                "            \"minor\": 0\n" +
                "            },\n" +
                "            \"ids\": [");
        for (int x = 0; x < facetses.size() - 1; x++) {
            sb.append("\"" + facetses.get(x).getFacetName() + "\",\n");
        }
        sb.append("\"" + facetses.get(facetses.size() - 1).getFacetName() + "\"\n");
        //sb.append("]}]}");
        sb.append("            ]\n" +
                "      }\n" +
                "  ]\n" +
                "}");
        return sb.toString();
    }

    /**
     * @return facets.uaf文件json串
     */
    public String getFacetsFileString2(String tanentid) {
        JSONObject jsonObject = new JSONObject();//总结点

        JSONArray trustedFacetsJsonArray = new JSONArray();
        JSONObject trustedFacetJsonObject = new JSONObject();//trustedFacets节点

        JSONObject versionJsonObject = new JSONObject();//编辑version节点
        versionJsonObject.put("major", 1);
        versionJsonObject.put("minor", 0);

        JSONArray idsJsonArray = new JSONArray();//ids节点
        List<Facets> facetsList = Facets.dao.findAll(tanentid);

        for (Facets facets : facetsList) {
            idsJsonArray.add(facets.getFacetName());
        }

        trustedFacetJsonObject.put("version", versionJsonObject);
        trustedFacetJsonObject.put("ids", idsJsonArray);

        trustedFacetsJsonArray.add(trustedFacetJsonObject);

        jsonObject.put("trustedFacets", trustedFacetsJsonArray);
        return jsonObject.toJSONString();
    }

    /**
     * 根据应用名称和渠道ID判断应用信息是否存在
     *
     * @param facetName
     * @param tenantId
     * @return BOOLEAN
     */
    public boolean exist(String facetName, String tenantId) {
        Facets facets = Facets.dao.getByNameAndTenantId(facetName, tenantId);
        return facets == null;
    }
}
